<template>
  <div style="padding: 20px">
    <div class="title_font">
      添加新闻
    </div>
    <div style="display: flex; justify-content: left">
      <span class="span_style" style="padding-top: 30px">封面图：</span>
      <el-upload
          class="avatar-uploader"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
      >
        <img v-if="imageUrl" :src="imageUrl" class="avatar" />
        <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
      </el-upload>
    </div>
    <div style="display: flex; justify-content: left;margin-top: 10px">
      <span class="span_style">新闻标题：</span>
      <el-input v-model="input" placeholder="请输入" style="width: 300px"/>
    </div>
    <div style="display: flex; justify-content: left;margin-top: 10px">
      <span class="span_style">新闻内容：</span>
      <el-input v-model="textarea" type="textarea" e="textarea" :rows="4" placeholder="请输入" style="width: 300px"/>
    </div>
    <div style="display: flex; justify-content: left;margin-top: 10px">
      <span class="span_style">发布状态：</span>
      <el-radio-group v-model="radio1" class="ml-4">
        <el-radio label="1" size="large">立即发布</el-radio>
        <el-radio label="2" size="large">隐藏</el-radio>
      </el-radio-group>
    </div>
    <div class="button_style">
      <button class="add_button">添加</button>
      <button class="chance_button">取消</button>
    </div>
  </div>

</template>
<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'
const radio1 = ref('1')
const textarea = ref('')
const imageUrl = ref('')

const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
  imageUrl.value = URL.createObjectURL(uploadFile.raw!)
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.type !== 'image/jpeg') {
    ElMessage.error('Avatar picture must be JPG format!')
    return false
  } else if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('Avatar picture size can not exceed 2MB!')
    return false
  }
  return true
}
</script>
<style lang="scss">

.button_style{
  margin-top: 20px;
  .add_button{
    width: 100px;
    background-color: #165dff;
    color: white;
    font-weight: 500;
    border: 0;
    height: 30px;
    border-radius: 3px;
  }
  .chance_button{
    margin-left: 20px;
    width: 100px;
    background-color: white;
    font-weight: 500;
    border: 1px solid black;
    height: 30px;
    border-radius: 3px;
  }
}
.span_style{
  font-size: 12px;
  padding-top: 10px;
  width: 80px;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 80px;
  height: 80px;
  text-align: center;
}
</style>
